package com.heifeng.cloudshelf.mvp.show.activity;

import android.content.Context;
import android.os.Handler;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.heifeng.cloudshelf.NetWorkReceiver;
import com.heifeng.cloudshelf.R;
import com.heifeng.cloudshelf.base.BaseActivity;
import com.heifeng.cloudshelf.event.OpenDetailWithEvent;
import com.heifeng.cloudshelf.mvp.show.contract.ShowHomeActivityContract;
import com.heifeng.cloudshelf.mvp.show.presenter.ShowHomeActivityPresenter;
import com.heifeng.cloudshelf.utils.IntentUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

/*
 *晒晒我家
 */
public class ShowHomeActivity extends BaseActivity<ShowHomeActivityPresenter> implements ShowHomeActivityContract.View {
    @BindView(R.id.l_error)
    View lError;//网络错误

    @BindView(R.id.l_no_data)
    View lNoData;//没有数据

    @BindView(R.id.r_top)
    RelativeLayout rTop;

    @BindView(R.id.recyclerview)
    RecyclerView recyclerView;

    @BindView(R.id.l_back)
    LinearLayout lBack;

    @BindView(R.id.r_layout)
    RelativeLayout rLayout;

    @BindView(R.id.tv_list_count)
    TextView tv_list_count;

    private TranslateAnimation translateAnimation;

    private TranslateAnimation translateAnimation1;

    private TranslateAnimation translateAnimation2;

    private TranslateAnimation translateAnimationOpenDetail;
    private String listCount = "0个方案结果";

    private boolean isCliecked = false;

    public static void open(Context context) {
        IntentUtil.startActivity(context, ShowHomeActivity.class);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_show_home;
    }

    @Override
    protected void initEventAndData() {
        EventBus.getDefault().register(this);

        onStartAnim();
        mPresenter.init();
        mPresenter.getBlueprintList();
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
//                Log.e("onScrolled","滑动暂停,newState："+newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    if (mPresenter.isVisBottom(recyclerView)) {
                        mPresenter.nextPage();
                    }
                }
            }
        });

        lError.findViewById(R.id.tv_refresh).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                checkNet();
                showNetError(isNetConnect());
                mPresenter.getBlueprintList();
            }
        });

        showNetError(isNetConnect());

        NetWorkReceiver.setOnNetConnectListener(isConnected -> showNetError(isConnected));
    }

    private void showNetError(boolean netConnect) {
        if (null != lError) {
            if (netConnect) {//进入界面获取网络状态
                if (View.VISIBLE == lError.getVisibility()) {
                    lError.setVisibility(View.GONE);
                    recyclerView.setVisibility(View.VISIBLE);
                    Log.e("TAGS", "count:" + listCount);
                    tv_list_count.setText(listCount);
                }
            } else {
                if (View.GONE == lError.getVisibility()) {
                    listCount = tv_list_count.getText().toString();
                    lError.setVisibility(View.VISIBLE);
                    recyclerView.setVisibility(View.GONE);
                    setListSize(0);
                }
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    public void onStartAnim() {
        translateAnimation = (TranslateAnimation) AnimationUtils.loadAnimation(ShowHomeActivity.this, R.anim.anim_bottom_to_top);
        translateAnimation1 = (TranslateAnimation) AnimationUtils.loadAnimation(ShowHomeActivity.this, R.anim.anim_bottom_to_top_slow);
        translateAnimation2 = (TranslateAnimation) AnimationUtils.loadAnimation(ShowHomeActivity.this, R.anim.anim_bottom_to_top_more_slow);

        rTop.startAnimation(translateAnimation);
        recyclerView.startAnimation(translateAnimation1);
        lBack.startAnimation(translateAnimation2);
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @OnClick({R.id.l_back})
    public void Onclick(View view) {
        switch (view.getId()) {
            case R.id.l_back:
                finish();
                break;
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        isCliecked = false;
    }


    @Override
    protected void onResume() {
        super.onResume();
        isCliecked = false;
//        if (null != rLayout) {
//            rLayout.setVisibility(View.VISIBLE);
//        }
    }

    @Override
    public RecyclerView getRecyclerView() {
        return recyclerView;
    }

    @Override
    public void setListSize(int count) {
        tv_list_count.setText(count + "个方案结果");
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void OpenDetailEvent(OpenDetailWithEvent event) {

        if (isCliecked) {
            return;
        }

        isCliecked = true;
        onOpenDetailAnim();

//        translateAnimationOpenDetail.setAnimationListener(new Animation.AnimationListener() {
//            @Override
//            public void onAnimationStart(Animation animation) {
//
//            }
//
//            @Override
//            public void onAnimationEnd(Animation animation) {
//                rLayout.setVisibility(View.GONE);
//                ShowDetailActivity.StartActivity(ShowHomeActivity.this, 1, event.getId(), event.getTitle());
//            }
//
//            @Override
//            public void onAnimationRepeat(Animation animation) {
//                Log.e("Animation","onAnimationRepeat");
//            }
//        });

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                ShowDetailActivity.StartActivity(ShowHomeActivity.this, 1, event.getId(), event.getTitle());
            }
        }, 800);    //延时
    }

    public void onOpenDetailAnim() {
        translateAnimationOpenDetail = (TranslateAnimation) AnimationUtils.loadAnimation(ShowHomeActivity.this, R.anim.anim_open_detail1);
        rLayout.startAnimation(translateAnimationOpenDetail);

    }
}
